package com.bawei.demo_shoppingcar.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import com.bawei.demo_shoppingcar.R;
import com.bawei.demo_shoppingcar.view.TabLayoutFragment;


public class TabLayoutActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout);
        initView();
        initData();
    }

    private void initData() {
        TabLayoutAdapter adapter = new TabLayoutAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void initView() {
        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);

    }

    class TabLayoutAdapter extends FragmentPagerAdapter{

        private String[] title = {
                "补水保湿",
                "美白提亮",
                "实惠套装",
                "贴士面膜",
                "控油祛痘",
                "洁面乳",
        };

        public TabLayoutAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }

        @Override
        public Fragment getItem(int position) {
            TabLayoutFragment fragment = new TabLayoutFragment(title[position],"");
            return fragment;
        }

        @Override
        public int getCount() {
            return title.length;
        }
    }
}
